﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MShop.Core.Domain;

namespace MShop.Core.DataAccess
{
    public class EmailTemplateDao : CommonDao<EmailTemplate>, IEmailTemplateDao
    {
        public EmailTemplateDao(IObjectContextProvider provider)
            : base(provider)
        {
        }


        #region IEmailTemplate Members

        public EmailTemplate GetByEmailEntity(Type type)
        {
            EmailTemplate template = null;
            this.GetAll().ToList().ForEach((x) => {
                 Type t= Type.GetType(x.EmailType);
                 if (t == type)
                 {
                     template = x;
                 }
            });           
            return template;
        }

        #endregion

        #region IEmailTemplateDao Members


        public EmailEntity GetEmailEntity(int id)
        {
            try
            {
                EmailTemplate template = GetById(id);
                var obj = System.Activator.CreateInstance(Type.GetType(template.EmailType));
                return (EmailEntity)obj;
            }
            catch
            {
                return null;
            }
        }

        #endregion
    }
}